use actix_web::{web, HttpResponse};
use chrono::Utc;
use sqlx_oldapi::types::chrono;
use sqlx_oldapi::{PgConnection, PgPool};
use uuid::Uuid;

#[derive(serde::Deserialize)]
pub struct FormData {
    email: String,
    name: String,
}

pub async fn subscribe(form: web::Form<FormData>, pool: web::Data<PgPool>) -> HttpResponse {
    sqlx_oldapi::query!(
        r#"
        INSERT INTO subscriptions (id, email, name, subscribed_at) VALUES ($1, $2, $3, $4)
    "#,
        Uuid::new_v4(),
        form.email,
        form.name,
        Utc::now()
    )
        .execute(pool.get_ref())
        .await
    {
        Ok(_) => HttpResponse::Ok().finish(),
        Err(e) => {
        println!("Failed to execute query: {}", e);
        HttpResponse::InternalServerError().finish()
    }
    };
    HttpResponse::Ok().finish()
}
